package com.ruoyi.points.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.points.domain.GroupingProduct;
import com.ruoyi.points.service.IGroupingProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 拼团Controller
 *
 * @author ruoyi
 * @date 2024-08-06
 */
@RestController
@RequestMapping("/groupingProduct")
public class GroupingProductController extends BaseController
{
    @Autowired
    private IGroupingProductService groupingProductService;

    /**
     * 查询拼团列表
     */
    @RequiresPermissions("points:groupingProduct:list")
    @GetMapping("/list")
    public TableDataInfo list(GroupingProduct groupingProduct)
    {
        startPage();
        List<GroupingProduct> list = groupingProductService.selectGroupingProductList(groupingProduct);
        return getDataTable(list);
    }

    /**
     * 导出拼团列表
     */
    @RequiresPermissions("points:groupingProduct:export")
    @Log(title = "拼团", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GroupingProduct groupingProduct)
    {
        List<GroupingProduct> list = groupingProductService.selectGroupingProductList(groupingProduct);
        ExcelUtil<GroupingProduct> util = new ExcelUtil<GroupingProduct>(GroupingProduct.class);
        util.exportExcel(response, list, "拼团数据");
    }

    /**
     * 获取拼团详细信息
     */
    @RequiresPermissions("points:groupingProduct:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        GroupingProduct data = groupingProductService.selectGroupingProductById(id);
//        获取规格关联id，这个时候是个字符串，逗号分割
        String specificationId = data.getSpecificationId();
        ArrayList<Object> list = new ArrayList<>();
        String[] split = specificationId.split(",");
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        data.setList(list);


        return success(data);
    }

    /**
     * 新增拼团
     */
    @RequiresPermissions("points:groupingProduct:add")
    @Log(title = "拼团", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GroupingProduct groupingProduct)
    {
        ArrayList<Object> list = groupingProduct.getList();
        String str= "";
        for (int i = 0; i < list.size(); i++) {
            if(i!=list.size()-1){
                str= str+list.get(i)+",";
            }else {
                str= str+list.get(i);
            }
        }
        groupingProduct.setSpecificationId(str);
        return toAjax(groupingProductService.insertGroupingProduct(groupingProduct));
    }

    /**
     * 修改拼团
     */
    @RequiresPermissions("points:groupingProduct:edit")
    @Log(title = "拼团", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GroupingProduct groupingProduct)
    {
        ArrayList<Object> list = groupingProduct.getList();
        String str= "";
        for (int i = 0; i < list.size(); i++) {
            if(i!=list.size()-1){
                str= str+list.get(i)+",";
            }else {
                str= str+list.get(i);
            }
        }
        groupingProduct.setSpecificationId(str);
        return toAjax(groupingProductService.updateGroupingProduct(groupingProduct));
    }

    /**
     * 删除拼团
     */
    @RequiresPermissions("points:groupingProduct:remove")
    @Log(title = "拼团", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(groupingProductService.deleteGroupingProductByIds(ids));
    }
}
